package Leetcode;

import java.util.Objects;

/**
 * @ Author     ：侯耀祖
 * @ Description：
 */
public class getIntersectionNode {
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        ListNode pA = headA, pB = headB;
        while (pA != pB) {
            pA = (pA == null) ? headB : pA.next;
            pB = (pB == null) ? headA : pB.next;
        }
        return pA; // 当 pA == pB 时即为交点（或同为 null）
    }
    public ListNode getIntersectionNode1(ListNode headA, ListNode headB) {
        int lenA=0,lenB=0;
        ListNode temp1 = headA;
        ListNode temp2 = headB;
        while(temp1!=null){
            lenA++;
            temp1 = temp1.next;
        }
        while(temp2!=null){
            lenB++;
            temp2 = temp2.next;
        }
        if (lenA > lenB){
            for (int i = 0; i < lenA - lenB; i++) {
                headA = headA.next;
            }
        } else {
            for (int i = 0; i < lenB - lenA; i++) {
                headB = headB.next;
            }
        }
        while (headA != null){
            if (headA == headB){
                return headA;
            } else {
                headA = headA.next;
                headB = headB.next;
            }
        }
        return null;
    }
}
